package com.itic.system.user.scavenger.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.appbase.framework.utils.WxUtils;
import com.itic.system.user.scavenger.persistence.dao.HppUserScavengerDao;
import com.itic.system.user.scavenger.persistence.model.HppUserScavenger;
import com.itic.system.user.scavenger.persistence.model.HppUserScavengerExample;
import com.itic.system.user.scavenger.persistence.model.HppUserScavengerExample.Criteria;

@Service
@Transactional
public class HppUserScavengerService
     extends CrudService<HppUserScavengerDao,HppUserScavenger,HppUserScavengerExample> {
    @Autowired
    private HppUserScavengerDao hppUserScavengerDao;

    public String addScavenger(String code,String userId,String appId,String appSecret){
    	String result = FAILED;
    	HppUserScavenger scavenger = new HppUserScavenger();
    	Map<String,String> map = WxUtils.getWXuserBycode(code,appId,appSecret);
    	String unionId = map.get("unionId");
    	String headImg = map.get("headimgurl");
    	String nickname = map.get("nickname");
    	scavenger.setHeadImg(headImg);
    	scavenger.setNickName(nickname);
    	scavenger.setUnionId(unionId);
    	scavenger.setUserId(userId);
    	result = this.add(scavenger);
    	return result;
    }
    
    /**
     * 校验操作权限
     * @param userId
     * @param id
     * @return
     */
    public Boolean isOwn(String userId,String id){
    	Boolean isOwn = false;
    	HppUserScavengerExample example = new HppUserScavengerExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	criteria.andIdEqualTo(id);
    	List<HppUserScavenger> list = this.list(example);
    	if(list.size()>0){
    		isOwn = true;
    	}
    	return isOwn;
    }
    /**
     * 取消扫码员
     * @param userId
     * @param id
     * @return
     */
    public String del(String userId,String id){
    	String result = FAILED;
    	HppUserScavenger scavenger = new HppUserScavenger();
    	scavenger.setUserId(userId);
    	scavenger.setId(id);
    	result = this.delete(scavenger);
    	return result;
    }
    
    /**
     * 根据用户Id获取扫码员列表
     * @param userId
     * @return
     */
    public List<HppUserScavenger> getlist(String userId){
    	HppUserScavengerExample example = new HppUserScavengerExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andIsEffectiveEqualTo(DBConst.TRUE);
    	criteria.andUserIdEqualTo(userId);
    	List<HppUserScavenger> list = this.hppUserScavengerDao.selectByExample(example);
    	return list;
    }
    
    
}